var express = require('express'),
    // swig = require('swig-templates'),
    // request = require('request'),
    wx = require('../base/wx'),
    // sha1 = require('node-sha1'),
    // crypto = require('crypto'),
    router = express.Router();

router.get('/',function(req, res,next) {
    wx.goAccessToken(req, res, function () {
        wx.getToken(req, function () {

            var result = {
                "code": 200,
                "kind": "mobile",
                "message": {
                    "template": {
                        "type": "diy",
                        "name": "ip6/6s-光面",
                        "basemap": "/uploads/basemap/20170720/49f91930b4fad4f1a0cb960ff2319460.png",
                        "widthMm": "87.88",
                        "heightMm": "146.05",
                        "width": 1038,
                        "height": 1725,
                        "offset_x": 581,
                        "offset_y": 238,
                        "image": "",
                        "if_no_image": {
                            "bg_width": 2200,
                            "bg_height": 2200,
                            "bg_color": "FFFFFF"
                        },
                        "left_image": "",
                        "right_image": "",
                        "up_image": "",
                        "down_image": "",
                        "origin_price": "48.00",
                        "preferential_price": "30.00"
                    },
                    "chartlet": [{
                        "type": "默认",
                        "content": [{
                            "chartlet_id": 2,
                            "name": "飞舞",
                            "type_id": 2,
                            "url": "/uploads/chartlet/20170713/c59fb2986ad981c3bdc0f8f11b9c86d1.png",
                            "type": "full"
                        }, {
                            "chartlet_id": 8,
                            "name": "tatoo",
                            "type_id": 2,
                            "url": "/uploads/chartlet/20170725/2a8597fb932663b83eca4ea284dee2bf.png",
                            "type": "full"
                        }, {
                            "chartlet_id": 10,
                            "name": "仙人掌",
                            "type_id": 2,
                            "url": "/uploads/chartlet/20170725/eb5460b175de4d949a2ce85b76046165.jpg",
                            "type": "full"
                        }, {
                            "chartlet_id": 11,
                            "name": "字母",
                            "type_id": 2,
                            "url": "/uploads/chartlet/20170725/44cb15ae60a9d8234138f067eed16a0e.jpg",
                            "type": "full"
                        }, {
                            "chartlet_id": 12,
                            "name": "扶桑花",
                            "type_id": 2,
                            "url": "/uploads/chartlet/20170725/750988b001465a9a3b3da212f002fa6f.jpg",
                            "type": "full"
                        }, {
                            "chartlet_id": 13,
                            "name": "火烈鸟",
                            "type_id": 2,
                            "url": "/uploads/chartlet/20170725/1199896735357cdad7bb24439969e016.jpg",
                            "type": "full"
                        }]
                    }],
                    "mask": [{
                        "type": "推荐",
                        "content": [{
                            "mask_id": 14,
                            "name": "萌",
                            "type_id": 1,
                            "url": "/uploads/mask/20180117/250fb27dfa08a5585c50efb72c9ce720.png"
                        }, {
                            "mask_id": 15,
                            "name": "火烈鸟",
                            "type_id": 1,
                            "url": "/uploads/mask/20180117/aa779a8ca5db6905036be1a7b349b04d.png"
                        }, {
                            "mask_id": 16,
                            "name": "仙人掌",
                            "type_id": 1,
                            "url": "/uploads/mask/20180317/bcf104030a117229591855065ea2d474.png"
                        }, {
                            "mask_id": 19,
                            "name": "怪物",
                            "type_id": 1,
                            "url": "/uploads/mask/20180322/3f74aa30473bf518d10e289e08d1592c.png"
                        }]
                    }]
                },
                "imsg": "返回数据成功",
                "isAction": 0
            };
            if (result.code == 200) {
                var rundData = {
                    message: result.message,
                    type: req.query.type
                };
                res.render('page/edit', rundData);

                // 实时证明，会执行下面的 log。
                // console.log('app/controllers/page/edit.js res.rend() 之后，测试是否会执行这里');
            }
            return;


            kakarotto.request.get({
                url: kakarotto.config.api.origin + '/index/index/edit?user_id=' + (req.cookies.user_id ? req.cookies.user_id : '0') + '&product_id=' + req.query.id + "&type=" + req.query.type
            }, function (error, response, body) {
                res.status(200).end('<h1>hello world</h1>');
                return;
                try {
                    var result = JSON.parse(body);
                    if (result.code == 200) {
                        var rundData = {
                            message: result.message,
                            type: req.query.type
                        };
                        res.render('page/edit', rundData);
                    }
                } catch (e) {
                    var err = new Error('Not Found');
                    err.status = 500;
                    next(err);
                    return;
                }
            });
        });
    });
});

module.exports = router;
